
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Arrays;

public class Client {
	
	/**
	 * send file to the next member
	 * @param nextMember next member on the member list
	 * @param fileName name of the file to be sent
	 * @throws Exception IO exception
	 */
	public void sendFile(String nextMember, String fileName) throws Exception {

		File file = new File(fileName);
		Socket socket = new Socket(nextMember, WebServer.PORT);
		ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
		// send file name first
		oos.writeObject(file.getName());
		FileInputStream fis = new FileInputStream(file);

		byte [] buffer = new byte[WebServer.BUFFER_SIZE];
		Integer bytesRead = 0;
		// send file content
		while ((bytesRead = fis.read(buffer)) > 0) {
			oos.writeObject(bytesRead);
			oos.writeObject(Arrays.copyOf(buffer, buffer.length));
		}
		oos.close();
	}

	/**
	 * tell the initiator that the all members have passed
	 * @param initiator user create the scheduling
	 * @param fileName the final common schedule file
	 * @throws Exception mostly IOexception
	 */
	public void schedulingDone(String initiator, String fileName) throws Exception {

		File file = new File(fileName);
		Socket socket = new Socket(initiator, WebServer.PORT);
		ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
		// send file name first - final schedule
		oos.writeObject(Constants.FINALSCHEDULE);
		FileInputStream fis = new FileInputStream(file);

		byte [] buffer = new byte[WebServer.BUFFER_SIZE];
		Integer bytesRead = 0;
		// send file content
		while ((bytesRead = fis.read(buffer)) > 0) {
			oos.writeObject(bytesRead);
			oos.writeObject(Arrays.copyOf(buffer, buffer.length));
		}
		oos.close();
	}

	/**
	 * tell the initiator that no common schedule has been found
	 * @param initiator user who created the scheduling
	 * @throws Exception mostly IO exception
	 */
	public void noCommonSchedule(String initiator) throws Exception{
		Socket socket = new Socket(initiator, WebServer.PORT);
		ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
		// tell initiator that no common schedule
		oos.writeObject(Constants.NOCOMMONSCHEDULE);
		oos.close();
	}

	/**
	 * ask a member his/her availability to participate a scheduling
	 * @param member member
	 * @param host user who tries to create a scheduling
	 * @throws Exception mostly IO exception
	 */
	public void availabilityCheck(String member, String host) throws Exception{
		Socket socket = new Socket(member, WebServer.PORT);
		ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
		// ask member his availability by sending host ip
		oos.writeObject(Constants.BUSYCHECK+host);
		oos.close();
	}
	
	/**
	 * return user current availability to the initiator
	 * @param initiator user who tries to create a scheduling
	 * @param host user who are replying
	 * @param status busy or not
	 * @throws Exception IO exception
	 */
	public void returnStatus(String initiator, String host, Boolean status) throws Exception{
		Socket socket = new Socket(initiator, WebServer.PORT);
		ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
		// return its lock status
		if(status)	// if it is busy
			oos.writeObject(status);
		else // not busy
			oos.writeObject(host+","+"false");
		oos.close();	
	}
	
	/**
	 * tell member that scheduling has been cancel due to either no common schedule or one member is not available
	 * @param member member
	 * @throws Exception IO exception
	 */
	public void cancelScheduling(String member) throws Exception{
		Socket socket = new Socket(member, WebServer.PORT);
		ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
		// tell member that scheduling cancel
		oos.writeObject(Constants.SCHEDULECANCEL);
		oos.close();
	}

	/**
	 * tell the member the meeting time
	 * @param member member
	 * @param meetingTime time of the next meeting
	 * @throws Exception IO exception
	 */
	public void tellMemberSchedule(String member, String meetingTime) throws Exception {
		Socket socket = new Socket(member, WebServer.PORT);
		ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
		// tell member that scheduling cancel
		oos.writeObject(meetingTime);
		oos.close();

	}
}  